package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.api.R;
import com.example.demo.domain.OrderSyncRequest;
import com.example.demo.service.OrderSyncService;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Shanks
 * @date 2020-05-17
 */
@RestController
@RequestMapping("/order/sync")
public class OrderSyncController {

    @Resource
    private OrderSyncService orderSyncService;
    @Resource
    private RestHighLevelClient restHighLevelClient;

    @PostMapping("/upsert")
    public R upsertOrder(@RequestBody OrderSyncRequest order) {
        return R.success(orderSyncService.upsert(order));
    }

    @PostMapping("/upsertBatch")
    public R upsertBatch(@RequestBody List<OrderSyncRequest> orderList) {
        orderSyncService.upsertBatch(orderList);
        return R.success();
    }

    @PostMapping("query")
    public OrderSyncRequest query(@RequestBody OrderSyncRequest request) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.wildcardQuery("orderNo", String.format("*%s*", request.getOrderNo())));
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(boolQueryBuilder);

        SearchRequest searchRequest = new SearchRequest("order");
        // type, 7.x统一为_doc
        searchRequest.types("_doc");
        searchRequest.source(sourceBuilder);
        SearchResponse response;

        try {
            response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits searchHits = response.getHits();
            SearchHit[] searchHitsHits = searchHits.getHits();
            TotalHits totalHits = searchHits.getTotalHits();
            for (SearchHit searchHit : searchHitsHits) {
                Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();

                OrderSyncRequest orderSyncRequest = JSON.parseObject(JSON.toJSONString(sourceAsMap), OrderSyncRequest.class);
                System.out.println(orderSyncRequest);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}